/*******************************************************
> Copyright (C) 2024 ==IOT== All rights reserved.
> File Name: enroll.c
> Author: sy
> Website:www.sy.org
> Created Time: 2024年09月20日 星期五 15时53分33秒
***************************************************/
// 管理员登录注册

#include "enroll.h"
#include "cJSON.h"

void Register()
{
    struct manager man;
    memset(&man, 0, sizeof(man));

    printf("input your id、name、password and number\n");
    scanf("%s %s %s %d", man.id, man.name, man.password, &man.number);

    printf("Please confirm your password: ");
    scanf("%s", man.password1);

    if (strcmp(man.password, man.password1) != 0)
    {
        printf("两次密码不一致，请重新输入\n");
        return;
    }

    strcpy(man.type, "MANAGER");
    strcpy(man.directives, "Register");

    if (input_cJSON_reg(man) == -1)
    {
        printf("JSON创建失败，注册失败\n");
        return;
    }

    if (sendto_and_receive(buf) == -1)
    {
        printf("数据发送失败\n");
        return;
    }

    output_cJSON_reg(buf);
}

int input_cJSON_reg(struct manager man)
{
    cJSON *json = cJSON_CreateObject();
    if (!json)
        return -1;

    cJSON_AddItemToObject(json, "CLI_Type", cJSON_CreateString(man.type));
    cJSON_AddItemToObject(json, "PackType", cJSON_CreateString(man.directives));
    cJSON_AddItemToObject(json, "CLI_ID", cJSON_CreateString(man.id));
    cJSON_AddItemToObject(json, "CLI_Name", cJSON_CreateString(man.name));
    cJSON_AddItemToObject(json, "CLI_Passwd", cJSON_CreateString(man.password));
    cJSON_AddItemToObject(json, "CLI_Number", cJSON_CreateNumber(man.number));

    char *dest = cJSON_Print(json);
    if (!dest)
    {
        cJSON_Delete(json);
        return -1;
    }

    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd == -1)
    {
        perror("socket");
        free(dest);
        cJSON_Delete(json);
        return -1;
    }

    struct sockaddr_in addr;
    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(PORT);
    addr.sin_addr.s_addr = inet_addr(IP);

    if (connect(sockfd, (struct sockaddr *)&addr, sizeof(addr)) == -1)
    {
        perror("connect");
        close(sockfd);
        free(dest);
        cJSON_Delete(json);
        return -1;
    }

    // 发送
    if (send(sockfd, dest, strlen(dest), 0) == -1)
    {
        perror("send error");
        close(sockfd);
        free(dest);
        cJSON_Delete(json);
        return -1;
    }

    // 接收
    char buf[1024] = {0};
    if (recv(sockfd, buf, sizeof(buf), 0) == -1)
    {
        perror("recv error");
        close(sockfd);
        free(dest);
        cJSON_Delete(json);
        return -1;
    }

    cJSON_Delete(json);
    free(dest);
    close(sockfd);

    // 解析返回的 JSON
    cJSON *json_response = cJSON_Parse(buf);
    if (!json_response)
    {
        printf("Failed to parse JSON response\n");
        return -1;
    }

    // 处理返回的 JSON
    cJSON *node = cJSON_GetObjectItem(json_response, "CLI_Type");
    if (!node || strcmp(node->valuestring, "MANAGER") != 0)
    {
        printf("error:返回的包错误\n");
        cJSON_Delete(json_response);
        return -1;
    }

    node = cJSON_GetObjectItem(json_response, "PackType");
    if (node && strcmp(node->valuestring, "Register") == 0)
    {
        node = cJSON_GetObjectItem(json_response, "ause");
        if (!node || node->type != cJSON_String)
        {
            printf("error:返回的包错误\n");
            cJSON_Delete(json_response);
            return -1;
        }

        if (strcmp(node->valuestring, "success") == 0)
        {
            printf("注册成功\n");
        }
        else
        {
            printf("注册失败\n");
            node = cJSON_GetObjectItem(json_response, "Info");
            if (node && node->type == cJSON_String)
            {
                printf("Info:%s\n", node->valuestring);
            }
            else
            {
                printf("error:返回的包错误\n");
            }
        }
    }

    cJSON_Delete(json_response);
    return 0; // 成功
}

void Login()
{
    struct manager man;
    memset(&man, 0, sizeof(man));

    printf("请输入账户id:密码\n");
    scanf("%d%s", &man.id, man.password);

    strcpy(man.type, "MANAGER");
    strcpy(man.directives, "Login");

    if (input_cJSON_login(man) == -1)
    {
        printf("JSON创建失败，登录失败\n");
        return;
    }

    if (sendto_and_receive(buf) == -1)
    {
        printf("数据发送失败\n");
        return;
    }

    output_cJSON_login(buf);
}

int input_cJSON_login(struct manager man)
{
    cJSON *json = cJSON_CreateObject();
    if (!json)
        return -1;

    cJSON_AddItemToObject(json, "CLI_Type", cJSON_CreateString(man.type));
    cJSON_AddItemToObject(json, "CLI_ID", cJSON_CreateNumber(man.id));
    cJSON_AddItemToObject(json, "CLI_Passwd", cJSON_CreateString(man.password));
    cJSON_AddItemToObject(json, "PackType", cJSON_CreateString(man.directives));

    char *dest = cJSON_Print(json);
    if (!dest)
    {
        cJSON_Delete(json);
        return -1;
    }

    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd == -1)
    {
        perror("socket");
        free(dest);
        cJSON_Delete(json);
        return -1;
    }

    struct sockaddr_in addr;
    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(PORT);
    addr.sin_addr.s_addr = inet_addr(IP);

    if (connect(sockfd, (struct sockaddr *)&addr, sizeof(addr)) == -1)
    {
        perror("connect");
        close(sockfd);
        free(dest);
        cJSON_Delete(json);
        return -1;
    }

    // 发送
    if (send(sockfd, dest, strlen(dest), 0) == -1)
    {
        perror("send error");
        close(sockfd);
        free(dest);
        cJSON_Delete(json);
        return -1;
    }

    // 接收
    char buf[1024] = {0};
    if (recv(sockfd, buf, sizeof(buf), 0) == -1)
    {
        perror("recv error");
        close(sockfd);
        free(dest);
        cJSON_Delete(json);
        return -1;
    }

    cJSON_Delete(json);
    free(dest);
    close(sockfd);

    // 解析返回的 JSON
    cJSON *json_response = cJSON_Parse(buf);
    if (!json_response)
    {
        printf("Failed to parse JSON response\n");
        return -1;
    }

    cJSON *node = cJSON_GetObjectItem(json_response, "PackType");
    if (!node || node->type != cJSON_String)
    {
        printf("error:返回的包错误\n");
        cJSON_Delete(json_response);
        return -1;
    }

    if (strcmp(node->valuestring, "success") == 0)
    {
        printf("登录成功\n");
        node = cJSON_GetObjectItem(json_response, "Info");
        if (node && node->type == cJSON_String)
        {
            printf("Info:%s\n", node->valuestring);
        }
        else
        {
            printf("error:返回的包错误\n");
        }
    }
    else if (strcmp(node->valuestring, "Default") == 0)
    {
        printf("登录失败\n");
        node = cJSON_GetObjectItem(json_response, "Info");
        if (node && node->type == cJSON_String)
        {
            printf("Info:%s\n", node->valuestring);
        }
        else
        {
            printf("error:返回的包错误\n");
        }
    }

    cJSON_Delete(json_response);
    return 0; // 成功
}

void query()
{
}
